﻿using MES.Process.Domain;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MES.Process.Infrastructure
{
    public class MESDBContext : DbContext
    {
        public MESDBContext(DbContextOptions options) : base(options)
        {
        }
        /// <summary>
        /// 工艺表
        /// </summary>
        public DbSet<Crafts> Crafts { get; set; }
        /// <summary>
        /// 工艺工序中间表
        /// </summary>
        public DbSet<Craft_Process> Craft_Processes { get; set; }
        /// <summary>
        /// 工序表
        /// </summary>
        public DbSet<Processes> Processes { get; set; }
        /// <summary>
        /// 工序分类表
        /// </summary>
        public DbSet<ProcessType> ProcessType { get; set; }
        /// <summary>
        /// 数据库表配置
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Crafts>().ToTable("Crafts");
            modelBuilder.Entity<Processes>().ToTable("Processes");
            modelBuilder.Entity<Craft_Process>().ToTable("Craft_Process");
            modelBuilder.Entity<Crafts>().HasQueryFilter(p => !p.IsDelete);
            modelBuilder.Entity<Processes>().HasQueryFilter(p => !p.IsDelete);
            modelBuilder.Entity<Craft_Process>().HasQueryFilter(p => !p.IsDelete);
            modelBuilder.Entity<ProcessType>().HasQueryFilter(p => !p.IsDelete);
            // 关键：指定该实体对应数据库视图
            //modelBuilder.Entity<Processes>(entity =>
            //{
            //    entity.ToView("v_Processes"); // 关联视图名
            //    entity.HasNoKey(); // 视图通常无主键，需显式声明
            //});
            //modelBuilder.Entity<Crafts>(entity =>
            //{
            //    entity.ToSqlQuery("v_Crafts"); 
            //    entity.HasNoKey(); 
            //});
        }
    }
}
